//
//  ChatRoomViewController.m
//  iPhoneXMPP
//
//  Created by XiongYingqi on 14-5-13.
//
//

#import "ChatRoomViewController.h"
#import "iPhoneXMPPAppDelegate.h"
#import "NSString+DDXML.h"


@interface ChatRoomViewController ()

@end

@implementation ChatRoomViewController

/**
* 存储已经打开的实例
*/
static NSMapTable *chatRooms;

- (iPhoneXMPPAppDelegate *)appDelegate {
    return (iPhoneXMPPAppDelegate *) [[UIApplication sharedApplication] delegate];
}

+ (id)getRoom:(XMPPUserCoreDataStorageObject *)withUser {
    if (chatRooms == nil) {
        chatRooms = [[NSMapTable alloc] init];
    }
    ChatRoomViewController *chatRoom = [chatRooms objectForKey:withUser];
    if (chatRoom == nil) {
        chatRoom = [[ChatRoomViewController alloc] initWithUser:withUser initWithNibName:nil bundle:nil];
        [chatRooms setObject:chatRoom forKey:withUser];
    }
    NSLog(@"chatRoom: %d", chatRoom);
    return chatRoom;
}

- (id)initWithUser:(XMPPUserCoreDataStorageObject *)withUser initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.withUser = withUser;

//        self.jidLabel.text = xmppJid.user;
        NSLog(@" -------- do --------- %d", self);
    }


    NSLog(@" -------- init --------- %d", self);
    return self;
}


- (void)viewWillAppear:(BOOL)animated {
//    self.jidLabel.text = @"jid";
    self.jidLabel.text = self.withUser.displayName;


//    CGRect frame = self.view.frame;
//    frame.origin.y += self.appDelegate.navigationController.view.frame.size.height;
//    frame.size.height -= self.appDelegate.navigationController.view.frame.size.height;
//
//    self.view.frame = frame;
//    NSLog(@"height===== %f", self.appDelegate.navigationController.view.frame.size.height);


//    self.jidLabel.backgroundColor = [UIColor blueColor];
    self.jidLabel.textColor = self.sendButton.currentTitleColor;
    NSLog(@" -------- viewWillAppear ---------");
    [super viewWillAppear:animated];
}

//- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//
//        // Custom initialization
//    }
//    return self;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
    //设置成NO表示当前控件响应后会传播到其他控件上，默认为YES。
    tapGestureRecognizer.cancelsTouchesInView = NO;
    //将触摸事件添加到当前view
    [self.view addGestureRecognizer:tapGestureRecognizer];
}

- (void)keyboardHide:(UITapGestureRecognizer *)tap {
    [self.messageField resignFirstResponder];
}


/**
* 点击return时隐藏键盘
*/
- (IBAction)hideKeyboard:(id)sender {
    [sender resignFirstResponder];
}

/**
* 点击空白时隐藏键盘
*/
- (IBAction)backViewTouch:(id)sender {
    NSLog(@"touch -------- ");
    // 发送resignFirstResponder.
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

- (IBAction)sendMessage:(id)sender {

    NSString *messageBody = self.messageField.text;
    if (messageBody != nil && ![[messageBody stringByTrimming] isEqualToString:@""]) {
        XMPPMessage *message = [XMPPMessage messageWithType:@"chat" to:self.withUser.jid];
        [message addBody:self.messageField.text];

        [[[self appDelegate] xmppStream] sendElement:message];
        self.messageField.text = @"";
    }

}


@end
